package cn.ecut.dzy.lock;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @program: juc
 * @description:
 * @author: DingZhenYun
 * @create: 2021-10-18 22:34
 **/
//第一步 创建资源类，定义属性和操作方法
class Share {
    private int number = 0;


    //创建Lock
    private Lock lock = new ReentrantLock();
    private Condition condition = lock.newCondition();

    //+1的方法
    public void incr() throws InterruptedException {
        lock.lock();
        try {
            //判断
            while (number != 0) {
                condition.await();
            }
            //干活
            number++;
            System.out.println(Thread.currentThread().getName() + " :: " + number);
            //通知
            condition.signalAll();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    //-1的方法
    public void decr() throws InterruptedException {
        lock.lock();
        try {
            //判断
            while (number != 1) {
                condition.await();
            }
            //干活
            number--;
            System.out.println(Thread.currentThread().getName() + " :: " + number);
            //通知
            condition.signalAll();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }
}

public class ThreadDemo2 {

    public static void main(String[] args) {
        {
            //第三步 创建多个线程，调用资源类的操作方法
            Share share = new Share();
            //创建线程
            new Thread(() -> {
                for (int i = 1; i <= 10; i++) {
                    try {
                        share.incr(); //+1
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }, "AA").start();

            new Thread(() -> {
                for (int i = 1; i <= 10; i++) {
                    try {
                        share.decr(); //-1
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }, "BB").start();

            new Thread(() -> {
                for (int i = 1; i <= 10; i++) {
                    try {
                        share.incr(); //+1
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }, "CC").start();

            new Thread(() -> {
                for (int i = 1; i <= 10; i++) {
                    try {
                        share.decr(); //-1
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }, "DD").start();
        }
    }
}
